<?php

namespace App\Model\Chat;

use App\Model\Model;
use App\Model\User\User;
use Hyperf\Database\Model\SoftDeletes;

/**
 * @property integer $id
 * @property boolean $types
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $delete_time
 * @property text $content
 * @property integer $user_id
 */
class ChatSession extends Model
{
    use SoftDeletes;


    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'chat_session';

    protected $fillable = [
        'user_id',
        'receiver_id',
        'types',
        'last_message_id',
    ];

    protected $appends = [
        'update_time_format'
    ];

    public function getUpdateTimeFormatAttribute()
    {
        return $this->checkAttributes('update_time', function ($update_time) {
            return date('Y-m-d H:i:s', $update_time);
        });
    }

    public function User()
    {
        return $this->hasOne(User::class, 'uid', 'user_id');
    }

    public function Receiver()
    {
        return $this->hasOne(User::class, 'uid', 'receiver_id');
    }

    public function Message()
    {
        return $this->hasOne(ChatMessage::class, 'id', 'last_message_id');
    }

    public function receiverFd()
    {
        return $this->hasOne(ChatFd::class, 'user_id', 'receiver_id');
    }

    public function ChatRecords()
    {
        return $this->hasMany(ChatRecords::class, 'receiver_id', 'receiver_id');
    }
}
